﻿using Sl.Log;
using ChholCmdPort;
using Sl.Duplex;



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic.Devices;
using System.Reflection;

namespace ChholConfig {
	public partial class ChholSerialportUI {
		public Logger log;
		IDuplex main_sp;
		public Cc9d.Cc9dCodec main_Codec;
		public ToolStripButton PortOnOffBTN = new ToolStripButton();
		public ToolStripButton ScanBTN = new ToolStripButton();
		public ToolStripComboBox PortSelectCB = new ToolStripComboBox();
		public event EventHandler ePortStatusChange;

		public bool is_port_running=> main_sp != null && main_sp.IsRunning;

		public ChholSerialportUI(Logger log) {
			this.log = log;
			PortOnOffBTN.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
			PortOnOffBTN.Enabled = true;
			PortOnOffBTN.Image = Properties.Resources.connected_30px;
			PortOnOffBTN.Name = "PortOnOffBTN";
			PortOnOffBTN.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
			PortOnOffBTN.Size = new System.Drawing.Size(28, 28);
			PortOnOffBTN.Text = "启动串口";
			PortOnOffBTN.Click += PortOnOffBTN_Click;

			ScanBTN.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
			ScanBTN.Image = Properties.Resources.radar_50px;
			ScanBTN.Name = "ScanBTN";
			ScanBTN.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
			ScanBTN.Size = new System.Drawing.Size(28, 28);
			ScanBTN.Text = "扫描串口";
			ScanBTN.Click += searchPort_Click;

			PortSelectCB.DropDownWidth = 480;
			PortSelectCB.Name = "PortSelectCB";
			PortSelectCB.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
			PortSelectCB.Size = new System.Drawing.Size(265, 31);
			PortSelectCB.SelectedIndexChanged += PortSelectCB_SelectedIndexChanged;
		}

		public void openFixPort(string port) {
			startPort(port);
			if (is_port_running) {
				PortSelectCB.Items.Clear();
				ComPortDesc cpd = new ComPortDesc(port) {
					info = "固定端口",
					avaliable = true,
				};
				PortSelectCB.Items.Add(cpd);
				PortSelectCB.SelectedIndex = 0;
				PortSelectCB.Enabled = false;
			}
		}


		private void searchPort_Click(object sender, EventArgs e) {
			searchPort();
		}
		public void searchPort() {
			SerialPortSelecter serial_port_selecter;
			closePort();
			PortSelectCB.Items.Clear();
			serial_port_selecter = new SerialPortSelecter();
			if (serial_port_selecter.Ports.Length == 0) {
				log.LogLine($"【报告】没有找到任何端口。");
				return;
			} else {
				int avaliable_port = serial_port_selecter.Avaliable_port_num;
				int totle_port = serial_port_selecter.Ports.Length;
				foreach (var cpd in serial_port_selecter.Ports) {
					PortSelectCB.Items.Add(cpd);
				}
				PortSelectCB.Enabled = true;
				if (avaliable_port != 0) {
					PortSelectCB.SelectedIndex = 0;
					log.LogLine($"【报告】搜索完成，找到{avaliable_port}/{totle_port}个端口，打开了{PortSelectCB.Text}。");
				} else {
					log.LogLine($"【报告】搜索完成，找到{avaliable_port}/{totle_port}个端口，没有打开任何端口。");
				}
			}

		}

		private void PortSelectCB_SelectedIndexChanged(object sender, EventArgs e) {
			if((PortSelectCB.SelectedItem as ComPortDesc) == null) {
				PortOnOffBTN.Enabled = false;
			} else {
				PortOnOffBTN.Enabled = true;
				startPort((PortSelectCB.SelectedItem as ComPortDesc).id);
			}
		}

		void startPort(string sp_name) {
			closePort();
			string warning;
			main_sp = new SerialPortDp(sp_name, out warning);
			main_sp.ePortStatusChange += Main_sp_ePortStatusChange;
			if (warning != "") {
				log.LogLine($"【报告】打开{main_sp.Name}失败，原因：{warning}。");
				main_sp = null;
				return;
			}
			main_sp.openPort();
			if (!is_port_running) {
				log.LogLine($"【报告】打开{main_sp?.Name}失败。");
				return;
			}
			main_Codec = new Cc9d.Cc9dCodec(main_sp) {
				Log = Program.Log,
			};
			refreshPortStatus();
		}
		void closePort() {
			if (main_sp != null) {
				log.LogLine($"【报告】关闭了{main_sp.Name}。");
				main_sp.ePortStatusChange -= Main_sp_ePortStatusChange;
				main_sp.closePort();
				main_sp = null;
				main_Codec?.Dispose();
			}
			refreshPortStatus();
		}

		private void Main_sp_ePortStatusChange(IDuplex obj) {
			if (obj.IsRunning) {
				log.LogLine($"【报告】成功打开了{main_sp.Name}。");

			} else {
				closePort();
			}
		}

		public bool is_port_open {
			get {
				if (main_sp == null) {
					return false;
				} else {
					return main_sp.IsRunning;
				}
			}
		}

		private void PortOnOffBTN_Click(object sender, EventArgs e) {
			if (is_port_open) {
				closePort();
			} else {
				startPort((PortSelectCB.SelectedItem as ComPortDesc).id);
			}
		}

		void refreshPortStatus() {
			if (is_port_open) {
				this.PortOnOffBTN.Image = global::ChholConfig.Properties.Resources.connected_30px;
				PortOnOffBTN.Text = "将端口关闭";
			} else {
				this.PortOnOffBTN.Image = global::ChholConfig.Properties.Resources.disconnected_30px;
				PortOnOffBTN.Text = "将端口打开";
			}
			ePortStatusChange?.Invoke(this, EventArgs.Empty);
		}



	}
}

